package test.匹配pmid图片相关;

import cn.hutool.db.Db;
import cn.hutool.db.Entity;
import cn.hutool.db.ds.DSFactory;
import cn.hutool.db.ds.druid.DruidDSFactory;
import lombok.SneakyThrows;
import utils.SmallTool;

import javax.sql.DataSource;
import java.util.List;

/**
 * @Author: gcr
 * @Date: 2022/4/18 13:32
 */
public class 获取服务器图片存在的pmid号2 {

    public static String insert = "dir.pmid_figure";


    @SneakyThrows
    public static void main(String[] args) {

        SmallTool.printMemory();

        // vm options -Xmx18660m -Xms12560m

        DSFactory.setCurrentDSFactory(new DruidDSFactory());
        DataSource ds = DSFactory.get("server");
        Db useServer = Db.use(ds);
        Db useLocal = Db.use();

        String dbName = "pubmed_temp.pmiddoidown_gcr";


        int startPmid = 0;
        String allDate = "select max(pmid) as pmid  from " + dbName;
        Entity query = useServer.query(allDate).get(0);
        SmallTool.printMess("最大的id 是" + query.getStr("pmid"));

        Integer count = query.getInt("pmid");
        int breakSize = 100000;
        int pageSize = count / breakSize + 1;
        SmallTool.printMess("全部页码+++++++++" + pageSize);
        SmallTool.printMess("即将开始请提前关闭如果想的话");
        Thread.sleep(10000);

        SmallTool.printMemory();
        SmallTool.printMess("任务开始+++++++++++++++++++++++++++++++++++++++++++++++++++++");

        for (int i = 1; i <= pageSize; i++) {
            SmallTool.printMemory();
            SmallTool.printMess("当前页码+++++" + i);
            int start = breakSize * (i - 1) + 1;
            int end = i * breakSize;
            SmallTool.printMess("开始id++" + start + "+++结束id++++" + end);
            if (end <= startPmid) {
                SmallTool.printMess("因为小于" + startPmid + " 略过了");
                continue;
            }
            String paramSql = "select pmid,status from " + dbName + " where  pmid between  " + start + " and " + end;
            List<Entity> querys = useServer.query(paramSql);
            querys.parallelStream().forEach(entity -> {
                Integer pmid = entity.getInt("pmid");
                Integer status = entity.getInt("status");
                try {
                    insertData(pmid, status, useLocal);
                } catch (Exception e) {
                    e.printStackTrace();
                }

            });
        }


    }

    @SneakyThrows
    public static void insertData(Integer pmid, Integer sta, Db use) {
        use.insert(new Entity().setTableName(insert)
                .set("pmid", pmid)
                .set("status", sta)
        );

    }
}
